﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Numeric text field that provides automatic keystroke filtering and numeric validation.
    /// </summary>
    [Description("Numeric text field that provides automatic keystroke filtering and numeric validation.")]
    public class ExtNumberField : ExtTextField  
    {
        public override List<ScriptReference> GetScriptReferences()
        {
            return base.GetScriptReferences();
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.form.NumberField";

            String[] properties = new String[]{
                "allowDecimals", "allowNegative", "baseChars", "decimalPrecision", "decimalSeparator", 
                "maxText",  "maxValue", "growMin", "minText", "minValue", "nanText"
            };

            Type component = typeof(ExtNumberField);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtNumberField.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }                        

            return list;
        }

        #region Public Properties

        /// <summary>
        /// False to disallow decimal values (defaults to true).
        /// </summary>
        [DefaultValue(true)]
        [Description("False to disallow decimal values (defaults to true).")]
        public Boolean? allowDecimals { get; set; }

        /// <summary>
        /// False to prevent entering a negative sign (defaults to true).
        /// </summary>
        [DefaultValue(true)]
        [Description("False to prevent entering a negative sign (defaults to true).")]
        public Boolean? allowNegative { get; set; }

        /// <summary>
        /// The base set of characters to evaluate as valid numbers (defaults to '0123456789').
        /// </summary>
        [DefaultValue("0123456789")]
        [Description("The base set of characters to evaluate as valid numbers (defaults to '0123456789').")]
        public String baseChars { get; set; }

        /// <summary>
        /// The maximum precision to display after the decimal separator (defaults to 2).
        /// </summary>
        [DefaultValue(2)]
        [Description("The maximum precision to display after the decimal separator (defaults to 2).")]
        public int decimalPrecision { get; set; }

        /// <summary>
        /// Character(s) to allow as the decimal separator (defaults to '.').
        /// </summary>
        [DefaultValue(".")]
        [Description("Character(s) to allow as the decimal separator (defaults to '.').")]
        public String decimalSeparator { get; set; }

        /// <summary>
        /// Error text to display if the maximum value validation fails (defaults to 'The maximum value for this field is {maxValue}').
        /// </summary>
        [DefaultValue("The maximum value for this field is {maxValue}")]
        [Description("Error text to display if the maximum value validation fails (defaults to 'The maximum value for this field is {maxValue}').")]
        public String maxText { get; set; }

        /// <summary>
        /// The maximum allowed value (defaults to Double.MaxValue)
        /// </summary>
        [DefaultValue(Double.MaxValue)]
        [Description("The maximum allowed value (defaults to Double.MaxValue)")]
        public Double? maxValue { get; set; }

        /// <summary>
        /// Error text to display if the minimum value validation fails (defaults to 'The minimum value for this field is {minValue}').
        /// </summary>
        [DefaultValue("The minimum value for this field is {minValue}")]
        [Description("Error text to display if the minimum value validation fails (defaults to 'The minimum value for this field is {minValue}').")]
        public Unit? minText { get; set; }

        /// <summary>
        /// The minimum allowed value (defaults to Double.MinValue)
        /// </summary>
        [DefaultValue(Double.MinValue)]
        [Description("The minimum allowed value (defaults to Double.MinValue)")]
        public Double? minValue { get; set; }

        /// <summary>
        /// Error text to display if the value is not a valid number. For example, this can happen if a valid character like '.' or '-' is left in the field with no number (defaults to '{value} is not a valid number').
        /// </summary>
        [DefaultValue("{value} is not a valid number")]
        [Description("Error text to display if the value is not a valid number. For example, this can happen if a valid character like '.' or '-' is left in the field with no number (defaults to '{value} is not a valid number').")]
        public String nanText { get; set; }        

        #endregion
    }
}
